package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by zurdan on 26.11.14.
 */
public class ClientLogic implements Runnable {
    public static final String IP = "127.0.0.1";
    public static final int PORT = 8888;

    @Override
    public  void run() {
        final Proxy proxy;
        try {
            proxy = new Proxy(new Transport(IP, PORT));
        } catch (TransportException e) {
            System.err.println("Server closed");
            return;
        }
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));

        Thread thread = new Thread() {
            @Override
            public void run() {
                boolean success = true;
                while (success) {
                    success = proxy.read();
                }
            }
        };
        thread.start();

        while(!(thread.getState() == Thread.State.TERMINATED)) {
            try {
                proxy.send(scanner.readLine());
            } catch (IOException e) {
                System.out.println("There are some problem with input message.");
            }
        }
    }
}
